package com.binya.lib.permission;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.binya.lib.permission.share.ByPermissionSharePreference;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ydh
 * Time 2023年06月05日
 */
public class ByPermissionTool {

    /**
     * 权限申请
     *
     * @param context
     * @param permissionStorage
     * @param code
     */
    public static void requestPermission(Activity context, String[] permissionStorage, int code) {
        ActivityCompat.requestPermissions(context, permissionStorage, code);
    }

    public static void requestPermission(Activity context, String permission, int code) {
        requestPermission(context, new String[]{permission}, code);
    }

    /**
     * 检测权限是否通过
     *
     * @param context
     * @param permissionStorage
     * @return
     */
    public static boolean isPermission(Context context, String[] permissionStorage) {
        for (String s : permissionStorage) {
            if (!isPermission(context, s)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isPermission(Context context, String permission) {
        return ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
    }

    //获取权限成功
    public final static int STATUS_SUCCESS = 0;
    //申请权限拒绝, 但是下次申请权限还会弹窗
    public final static int STATUS_REFUSE = 1;
    //申请权限拒绝，并且是永久，不会再弹窗
    public final static int STATUS_REFUSE_PERMANENT = 2;
    //默认未请求授权状态
    public final static int STATUS_DEFAULT = 3;

    /**
     * 获取权限状态
     */
    public static int getAuthorizeStatus(Activity activity, String[] authorize) {
        for (String s : authorize) {
            int authorizeStatus = getAuthorizeStatus(activity, s);
            if (authorizeStatus != STATUS_SUCCESS) {
                return authorizeStatus;
            }
        }
        return STATUS_SUCCESS;
    }

    public static int getAuthorizeStatus(Activity activity, String authorize) {
        if (isPermission(activity, authorize)) {
            return STATUS_SUCCESS;
        }
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, authorize)) {
            // 判断用户是否拒绝了权限
            return STATUS_REFUSE;
        }
        if (ByPermissionSharePreference.getInstance(activity).getPermissionStatus().contains(authorize)) {
            return STATUS_REFUSE_PERMANENT;
        }
        return STATUS_DEFAULT;
    }

    /**
     * 权限请求返回操作（公用方法）
     *
     * @param permissions
     * @param grantResults
     */
    public static void onCommonRequestPermissionsResult(String[] permissions, int[] grantResults, PermissionCallback callback) {
        if (permissions == null) {
            return;
        }
        List<String> granted = new ArrayList<>();
        List<String> denied = new ArrayList<>();
        for (int i = 0; i < permissions.length; i++) {
            String perm = permissions[i];
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                granted.add(perm);
            } else {
                denied.add(perm);
            }
        }
        if (callback == null) {
            return;
        }
        if (!granted.isEmpty() && granted.size() == permissions.length) {
            callback.onPermissionsGranted(granted);
        }
        if (!denied.isEmpty()) {
            callback.onPermissionsDenied(denied);
        }
    }

    /**
     * 权限申请返回判断
     *
     * @param permissions
     * @param grantResults
     */
    public static void onRequestPermissionsResult(Activity activity, String[] permissions, int[] grantResults, PermissionCallback callback) {
        if (permissions == null) {
            return;
        }
        if (callback == null) {
            return;
        }
        List<String> granted = new ArrayList<>();
        List<String> denied = new ArrayList<>();
        for (int i = 0; i < permissions.length; i++) {
            String perm = permissions[i];
            if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                granted.add(perm);
            } else {
                denied.add(perm);
            }
        }
        if (!granted.isEmpty() && granted.size() == permissions.length) {
            for (String permission : permissions) {
                ByPermissionSharePreference.getInstance(activity).delPermissionStatus(permission + ",");
            }
            callback.onPermissionsGranted(granted);
        }
        if (!denied.isEmpty()) {
            for (String permission : permissions) {
                ByPermissionSharePreference.getInstance(activity).savePermissionStatus(permission + ",");
            }
            callback.onPermissionsDenied(denied);
        }
    }

    public interface PermissionCallback {

        /**
         * 用户同意返回
         * @param perms
         */
        void onPermissionsGranted(@NonNull List<String> perms);

        /**
         * 用户拒绝返回
         * @param perms
         */
        void onPermissionsDenied(@NonNull List<String> perms);
    }

}
